package Beans;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@ManagedBean
@SessionScoped
public class UserBean implements Serializable{
	private static final long serialVersionUID = 1L;
	private String firstName;
	private String lastName;
	private int age;
	private String email;
	private String login;
	private String pwd;
	private String rePwd;
	
	public UserBean() {
	}
	
	public UserBean(String firstName, String lastName, int age, String email,
			String login, String pwd, String rePwd) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
		this.email = email;
		this.login = login;
		this.pwd = pwd;
		this.rePwd = rePwd;
	}
	
	private String hashMe(String password) {
		 
        MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("SHA-256");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
        md.update(password.getBytes());
 
        byte byteData[] = md.digest();
 
        //Code extrait de http://www.mkyong.com/java/java-sha-hashing-example/
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
         sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
 
		return sb.toString();
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public int getAge() {
		return age;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getLogin() {
		return login;
	}
	
	public void setLogin(String login) {
		this.login = login;
	}
	
	public String getPwd() {
		return pwd;
	}
	
	public void setPwd(String pwd) {
		this.pwd = hashMe(pwd);
	}
	
	public String getRePwd() {
		return rePwd;
	}
	
	public void setRePwd(String rePwd) {
		this.rePwd = hashMe(rePwd);
	}
}
